package cn.yangliu.nacos.management.web.oauth2;

import java.util.Optional;

import cn.yangliu.jpa.handler.JpaHandler;
import cn.yangliu.nacos.comm.config.security.annotations.Authorize;
import cn.yangliu.nacos.management.entity.oauth2.Oauth2Client;
import cn.yangliu.nacos.management.model.ClientInsertModel;
import cn.yangliu.nacos.management.model.ClientQueryModel;
import cn.yangliu.nacos.management.model.ClientUpdateModel;
import cn.yangliu.nacos.management.service.oauth2.Oauth2ClientService;
import cn.yangliu.operation.annotation.Action;
import cn.yangliu.operation.annotation.Module;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
@RequestMapping("/client")
@RestController
@Api(value = "客户端管理", tags = "客户端管理")
@Module("oauth2管理-客户端管理")
public class Oauth2ClientController {

    /**
     * current class instance's member.
     * The Client service.
     */
    @Autowired
    private Oauth2ClientService clientService;

    /**
     * current class instance's member.
     * The Jpa handler.
     */
    private JpaHandler jpaHandler = JpaHandler.INSTANCE;

    /**
     * Insert oauth 2 client.
     *
     * @param model the model
     * @return the oauth 2 client
     */
    @PostMapping
    @ApiOperation("新增客户端")
    @Authorize("oauth2client:insert")
    @Action("新增客户端")
    public Oauth2Client insert(@RequestBody @Validated ClientInsertModel model) {
        return clientService.insert(model);
    }

    /**
     * Update oauth 2 client.
     *
     * @param model the model
     * @return the oauth 2 client
     */
    @PutMapping
    @ApiOperation("修改客户端")
    @Authorize("oauth2client:update")
    @Action("修改客户端")
    public Oauth2Client update(@RequestBody @Validated ClientUpdateModel model) {
        return clientService.update(model);
    }

    /**
     * Delete.
     *
     * @param id the id
     */
    @DeleteMapping("/{id}")
    @Action("删除客户端")
    @ApiOperation("删除客户端")
    @Authorize("oauth2client:delete")
    public void delete(@PathVariable("id") Long id) {
        clientService.deleteById(id);
    }

    /**
     * Find one oauth 2 client.
     *
     * @param id the id
     * @return the oauth 2 client
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询客户端")
    @Authorize("oauth2client:query")
    public Oauth2Client findOne(@PathVariable("id") Long id) {
        Optional<Oauth2Client> optional = clientService.findById(id);
        return optional.orElse(null);
    }

    /**
     * Page page.
     *
     * @param model the model
     * @return the page
     */
    @GetMapping("/page")
    @ApiOperation("分页查询客户端")
    @Authorize("oauth2client:query")
    public Page<Oauth2Client> page(@Validated ClientQueryModel model) {
        Specification<Oauth2Client> specification = jpaHandler.buildSpecification(model);
        return clientService.findAll(specification, model.build());
    }

}
